package com.example.service.impl;

import com.example.common.AppResult;
import com.example.common.ResultCode;
import com.example.dao.BoardMapper;
import com.example.exception.ApplicationException;
import com.example.model.Board;
import com.example.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-10-10
 * Time: 12:52
 */
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;

    /**
     * 根据编号查询板块信息
     * @param num 板块编号
     * @return 板块信息
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        //1.非空校验
        if(num <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用DAO层查询数据库获取对象
        List<Board> boards = boardMapper.selectByNum(num);
        //3.返回结果
        return boards;
    }

    /**
     * 根据板块id增加板块下的帖子数
     * @param id 帖子id
     */
    @Override
    public void addOneArticleCountById(Long id) {
        //1.非空校验
        if(id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //2.调用DAO层查询对应的板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //3.更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int updateCount = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(updateCount != 1){
            //打印日志
            log.warn(ResultCode.FAILED.toString()+"，受影响的行数不等于1");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    /**
     * 根据板块id查询板块信息
     * @param boardId 板块id
     * @return 板块信息
     */
    @Override
    public Board selectById(Long boardId) {
        //1.非空校验
        if(boardId == null || boardId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用DAO层查询数据库获取对象
        Board board = boardMapper.selectByPrimaryKey(boardId);
        if(board == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return board;
    }

    /**
     * 根据板块id减少板块下的帖子数
     * @param id 帖子id
     */
    @Override
    public void subOneArticleCountById(Long id) {
        //1.非空校验
        if(id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //2.调用DAO层查询对应的板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //3.更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        if(updateBoard.getArticleCount() < 0){
            //说明对应板块下的帖子数为0，直接设置版块下帖子数为0
            updateBoard.setArticleCount(0);
        }
        //4.调用DAO层更新数据库
        int updateCount = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(updateCount != 1){
            //打印日志
            log.warn(ResultCode.FAILED.toString()+"，受影响的行数不等于1");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
